home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CU Amiga Super CD-ROM 21
/
CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso
/
CUCD
/
Magazine
/
C_Tutorial
/
Part-6
/
asl2
/
gadgets.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-10-27
|
3KB
|
133 lines
#include "gadgets.h"
#include "screen.h"
#include "toolwin.h"
#include "visual.h"
#include<graphics/text.h>
#include<stdio.h>
#include<clib/gadtools_protos.h>
#include<clib/graphics_protos.h>
/* Some constants for the position and size of our gadgets */
#define MYBUT_LEFT (10)
#define MYBUT_TOP (5)
#define MYBUT_WIDTH (80)
#define MYBUT_HEIGHT (12)
#define MYBUT_TEXT "Next Pen"
#define MYPAL_LEFT (170)
#define MYPAL_TOP (1)
#define MYPAL_WIDTH (109)
#define MYPAL_HEIGHT (19)
#define MYPAL_TEXT "Colour:"
/* The initial pen colour */
#define MYINITPEN (1)
/* Global record of the complete gadget list */
static struct Gadget* glist = NULL;
/* Global record of the palette gadget */
static struct Gadget* palgad = NULL;
/* Initialised structure declaration: describes 8pt Topaz font */
static struct TextAttr topazFont = { "topaz.font", 8, 0, 0, };
/* Global record of current pen */
static UBYTE pen = MYINITPEN;
int gadgetHeight()
{
/* Palette gadget dictates vertical space needed */
return MYPAL_HEIGHT+(2*MYPAL_TOP);
}
/* Set foreground pen of window */
void setFgPen(struct Window* win, UBYTE value)
{
/* Wrap when reached the end of the palette gadget's colours */
pen = value % (1<<win->WScreen->BitMap.Depth);
SetAPen(win->RPort, pen);
if(palgad)
GT_SetGadgetAttrs(palgad, getToolWin(), NULL, GTPA_Color, pen, TAG_DONE);
}
void nextFgPen(struct Window* win)
{
setFgPen(win, pen+1);
}
void prevFgPen(struct Window* win)
{
setFgPen(win, pen-1);
}
void resetFgPen(struct Window* win)
{
setFgPen(win, MYINITPEN);
}
int createGadgets()
{
struct Gadget* gad;
struct Screen* scr = getScreen();
APTR vinfo = getVisual();
/* The offsets of our window borders */
int offleft = scr->WBorLeft;
int offtop = scr->WBorTop + (scr->Font->ta_YSize + 1);
struct NewGadget newgad;
/* Start a GadTools gadget list */
gad = CreateContext(&glist);
/* Setup our first gadget */
newgad.ng_TextAttr = &topazFont;
newgad.ng_VisualInfo = vinfo;
newgad.ng_LeftEdge = MYBUT_LEFT + offleft;
newgad.ng_TopEdge = MYBUT_TOP + offtop;
newgad.ng_Width = MYBUT_WIDTH;
newgad.ng_Height = MYBUT_HEIGHT;
newgad.ng_GadgetText = MYBUT_TEXT;
newgad.ng_GadgetID = MYBUT_ID;
newgad.ng_Flags = 0;
/* Now create it and add it to our list */
gad = CreateGadget(BUTTON_KIND, gad, &newgad, TAG_END);
/* Setup our second gadget */
/* (We can reuse newgad, and just change the different bits) */
newgad.ng_LeftEdge = MYPAL_LEFT + offleft;
newgad.ng_TopEdge = MYPAL_TOP + offtop;
newgad.ng_Width = MYPAL_WIDTH;
newgad.ng_Height = MYPAL_HEIGHT;
newgad.ng_GadgetText = MYPAL_TEXT;
newgad.ng_GadgetID = MYPAL_ID;
newgad.ng_Flags = 0;
/* Now create it and add it to our list */
gad = CreateGadget( PALETTE_KIND, gad, &newgad,
/* Initially selected pen */
GTPA_Color, pen,
/* Depth: 2 to the power depth colours */
GTPA_Depth, scr->BitMap.Depth,
/* Gadget will indicate selection */
GTPA_IndicatorWidth, 16,
TAG_DONE);
/* Remember gadget pointer */
palgad = gad;
/* Success if we managed final allocation */
return (gad != NULL);
}
void freeGadgets()
{
if(glist)
{
FreeGadgets(glist);
glist = NULL;
/* palgad is invalid now, too, so it must be set to NULL */
palgad = NULL;
}
}
struct Gadget* getGadgets()
{
return glist;
}